import 'dart:io';

import 'package:flutter/material.dart';
import 'package:charts_flutter/flutter.dart' as charts;
import '../../../app_files.dart';
import 'PieOutsideLabelChart.dart';
import '../model/main_view_model.dart';

List<charts.Color> chartsColor = [
  charts.Color(r: 108, g: 153, b: 250, a: 255),
  charts.Color(r: 252, g: 117, b: 106, a: 255),
  charts.Color(r: 254, g: 193, b: 98, a: 255),
  charts.Color(r: 47, g: 193, b: 156, a: 255),
  charts.Color(r: 242, g: 99, b: 123, a: 255),
];

List<Color> colors = [
  Color.fromRGBO(108, 153, 250, 1),
  Color.fromRGBO(252, 117, 106, 1),
  Color.fromRGBO(254, 193, 98, 1),
  Color.fromRGBO(47, 193, 156, 1),
  Color.fromRGBO(242, 99, 123, 1),
];

class UserNumberPieWidget extends StatelessWidget {
  final CountLeaderModel model;
  final bool isTotal;

  const UserNumberPieWidget(this.model, this.isTotal);

  List<charts.Series<LinearSales, String>> _createData() {
    List<LinearSales> data = [];
    if (isTotal) {
      data.add(LinearSales(
          '普表总数', double.tryParse(model.numberPB) ?? 0, chartsColor[0]));
      data.add(LinearSales(
          '卡表总数', double.tryParse(model.numberKB) ?? 0, chartsColor[1]));
      data.add(LinearSales(
          '物联网表总数', double.tryParse(model.numberYCB) ?? 0, chartsColor[2]));
    } else {
      data.add(LinearSales(
          '新增普表', double.tryParse(model.newOpenPB) ?? 0, chartsColor[0]));
      data.add(LinearSales(
          '新增卡表', double.tryParse(model.newOpenKB) ?? 0, chartsColor[1]));
      data.add(LinearSales(
          '新增物联网表', double.tryParse(model.newOpenYCB) ?? 0, chartsColor[2]));
    }

    double total = 0;
    for (LinearSales s in data) total += s.sales;

    data = total != 0
        ? data
        : [LinearSales('全部', 1, charts.Color(r: 108, g: 153, b: 250, a: 255))];

    return [
      new charts.Series<LinearSales, String>(
        id: 'Sales',
        domainFn: (LinearSales sales, _) => sales.title,
        measureFn: (LinearSales sales, _) => sales.sales,
        data: data,
        // Set a label accessor to control the text of the arc label.
        colorFn: (LinearSales row, _) => row.colors,
        leaderLineStyleAccessorFn: (LinearSales row, _) =>
            charts.ArcLabelLeaderLineStyleSpec(
                color: row.colors, length: 20, thickness: 1.5),
        outsideLabelStyleAccessorFn: (LinearSales row, _) =>
            charts.TextStyleSpec(
          color: charts.Color(r: 51, g: 51, b: 51, a: 255),
          fontSize: 13,
        ),
        labelAccessorFn: (LinearSales row, _) => total == 0
            ? ''
            : row.sales == 0
                ? ''
                : '${(row.sales / total * 100).toStringAsFixed(0)}%',
      )
    ];
  }

  List<Widget> _indicators() {
    List<Widget> list = [];
    if (isTotal) {
      list.add(
        Expanded(
          child: Align(
            child: Row(
              children: [
                Text(
                  '用户总数',
                  style: TextThemeConfig.Font13WithBigColor22,
                ),
                Padding(
                  padding: EdgeInsets.only(left: 49),
                  child: Text(
                    model.totalUser,
                    style: TextThemeConfig.Font15WithBigColor22,
                  ),
                ),
              ],
            ),
          ),
        ),
      );
      list.add(
        Expanded(
          child: Align(
            child: Row(
              children: [
                Container(
                  height: 10,
                  width: 10,
                  margin: EdgeInsets.only(right: 5),
                  decoration: BoxDecoration(
                    color: colors[0],
                    borderRadius: BorderRadius.all(Radius.circular(5)),
                  ),
                ),
                Text(
                  '普表总数',
                  style: TextThemeConfig.Font13WithColor99,
                ),
                Padding(
                  padding: EdgeInsets.only(left: 35),
                  child: Text(
                    model.numberPB,
                    style: TextThemeConfig.Font15WithBigColor22,
                  ),
                ),
              ],
            ),
          ),
        ),
      );
      list.add(
        Expanded(
          child: Align(
            child: Row(
              children: [
                Container(
                  height: 10,
                  width: 10,
                  margin: EdgeInsets.only(right: 5),
                  decoration: BoxDecoration(
                    color: colors[1],
                    borderRadius: BorderRadius.all(Radius.circular(5)),
                  ),
                ),
                Text(
                  '卡表总数',
                  style: TextThemeConfig.Font13WithColor99,
                ),
                Padding(
                  padding: EdgeInsets.only(left: 35),
                  child: Text(
                    model.numberKB,
                    style: TextThemeConfig.Font15WithBigColor22,
                  ),
                ),
              ],
            ),
          ),
        ),
      );
      list.add(
        Expanded(
          child: Align(
            child: Row(
              children: [
                Container(
                  height: 10,
                  width: 10,
                  margin: EdgeInsets.only(right: 5),
                  decoration: BoxDecoration(
                    color: colors[2],
                    borderRadius: BorderRadius.all(Radius.circular(5)),
                  ),
                ),
                Text(
                  '物联网表总数',
                  style: TextThemeConfig.Font13WithColor99,
                ),
                Padding(
                  padding: EdgeInsets.only(left: 10),
                  child: Text(
                    model.numberYCB,
                    style: TextThemeConfig.Font15WithBigColor22,
                  ),
                ),
              ],
            ),
          ),
        ),
      );
    } else {
      list.add(
        Expanded(
          child: Align(
            child: Row(
              children: [
                Text(
                  '今日新增用户',
                  style: TextThemeConfig.Font13WithBigColor22,
                ),
                Padding(
                  padding: EdgeInsets.only(left: 25),
                  child: Text(
                    model.newOpenToday,
                    style: TextThemeConfig.Font15WithBigColor22,
                  ),
                ),
              ],
            ),
          ),
        ),
      );
      list.add(
        Expanded(
          child: Align(
            child: Row(
              children: [
                Container(
                  height: 10,
                  width: 10,
                  margin: EdgeInsets.only(right: 5),
                  decoration: BoxDecoration(
                    color: colors[0],
                    borderRadius: BorderRadius.all(Radius.circular(5)),
                  ),
                ),
                Text(
                  '新增普表',
                  style: TextThemeConfig.Font13WithColor99,
                ),
                Padding(
                  padding: EdgeInsets.only(left: 36),
                  child: Text(
                    model.newOpenPB,
                    style: TextThemeConfig.Font15WithBigColor22,
                  ),
                ),
              ],
            ),
          ),
        ),
      );
      list.add(
        Expanded(
          child: Align(
            child: Row(
              children: [
                Container(
                  height: 10,
                  width: 10,
                  margin: EdgeInsets.only(right: 5),
                  decoration: BoxDecoration(
                    color: colors[1],
                    borderRadius: BorderRadius.all(Radius.circular(5)),
                  ),
                ),
                Text(
                  '新增卡表',
                  style: TextThemeConfig.Font13WithColor99,
                ),
                Padding(
                  padding: EdgeInsets.only(left: 36),
                  child: Text(
                    model.newOpenKB,
                    style: TextThemeConfig.Font15WithBigColor22,
                  ),
                ),
              ],
            ),
          ),
        ),
      );
      list.add(
        Expanded(
          child: Align(
            child: Row(
              children: [
                Container(
                  height: 10,
                  width: 10,
                  margin: EdgeInsets.only(right: 5),
                  decoration: BoxDecoration(
                    color: colors[2],
                    borderRadius: BorderRadius.all(Radius.circular(5)),
                  ),
                ),
                Text(
                  '新增物联网表',
                  style: TextThemeConfig.Font13WithColor99,
                ),
                Padding(
                  padding: EdgeInsets.only(left: 10),
                  child: Text(
                    model.newOpenYCB,
                    style: TextThemeConfig.Font15WithBigColor22,
                  ),
                ),
              ],
            ),
          ),
        ),
      );
    }
    return list;
  }

  @override
  Widget build(BuildContext context) {
    return Row(
      children: [
        Expanded(
          child: PieOutsideLabelChart(
            _createData(),
            animate: false,
            arcRatio: 0.4,
            outSideLabel: true,
          ),
          flex: 6,
        ),
        Expanded(
          flex: 5,
          child: Padding(
            padding: EdgeInsets.symmetric(vertical: 20),
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              crossAxisAlignment: CrossAxisAlignment.start,
              children: _indicators(),
            ),
          ),
        ),
      ],
    );
  }
}

class MainViewPieWidget extends StatelessWidget {
  final MainViewModel model;
  final int typeStatus;

  const MainViewPieWidget(this.model, this.typeStatus);

  List<charts.Series<LinearSales, String>> _createData() {
    List<LinearSales> data = [];
    if (typeStatus == 2) {
      data.add(
          LinearSales('新任务', model.newFixOrder.toDouble(), chartsColor[0]));
      data.add(
          LinearSales('已接任务', model.unFixOrder.toDouble(), chartsColor[1]));
      data.add(
          LinearSales('维修中', model.fixingOrder.toDouble(), chartsColor[2]));
      data.add(LinearSales('已维修', model.fixedOrder.toDouble(), chartsColor[3]));
    } else if (typeStatus == 6) {
      data.add(
          LinearSales('未审核', model.generalNumber.toDouble(), chartsColor[0]));
      data.add(
          LinearSales('审核通过', model.normalNumber.toDouble(), chartsColor[3]));
      data.add(
          LinearSales('审核拒绝', model.seriousNumber.toDouble(), chartsColor[4]));
      data.add(LinearSales(
          '待抄表', double.tryParse(model.unCheck) ?? 0.0, chartsColor[1]));
    } else if (typeStatus == 1) {
      data.add(
          LinearSales('正常', model.normalNumber.toDouble(), chartsColor[0]));
      data.add(LinearSales('自修', model.fixNumber.toDouble(), chartsColor[1]));
      data.add(
          LinearSales('隐患', model.generalNumber.toDouble(), chartsColor[2]));
      data.add(
          LinearSales('维修', model.seriousNumber.toDouble(), chartsColor[4]));
    } else if (typeStatus == 5) {
      data.add(
          LinearSales('新任务', model.newFixOrder.toDouble(), chartsColor[0]));
      data.add(
          LinearSales('已接任务', model.unFixOrder.toDouble(), chartsColor[1]));
      data.add(
          LinearSales('安装中', model.fixingOrder.toDouble(), chartsColor[2]));
      data.add(LinearSales('已安装', model.fixedOrder.toDouble(), chartsColor[3]));
    } else if (typeStatus == 7) {
      data.add(LinearSales(
          '进行中', double.tryParse(model.unCheck) ?? 0.0, chartsColor[0]));
      data.add(LinearSales('已接任务', model.allCheck.toDouble(), chartsColor[1]));
    } else {
      data.add(
          LinearSales('正常', model.normalNumber.toDouble(), chartsColor[0]));
      data.add(LinearSales('自修', model.fixNumber.toDouble(), chartsColor[1]));
      data.add(
          LinearSales('隐患', model.generalNumber.toDouble(), chartsColor[2]));
      data.add(
          LinearSales('维修', model.seriousNumber.toDouble(), chartsColor[4]));
    }

    double total = 0;
    for (LinearSales s in data) total += s.sales;

    data = total != 0
        ? data
        : [LinearSales('全部', 1, charts.Color(r: 108, g: 153, b: 250, a: 255))];

    return [
      new charts.Series<LinearSales, String>(
        id: 'Sales',
        domainFn: (LinearSales sales, _) => sales.title,
        measureFn: (LinearSales sales, _) => sales.sales,
        data: data,
        // Set a label accessor to control the text of the arc label.
        colorFn: (LinearSales row, _) => row.colors,
        outsideLabelStyleAccessorFn: (LinearSales row, _) =>
            charts.TextStyleSpec(
              color: charts.Color(r: 108, g: 153, b: 250, a: 255),
            ),
      )
    ];
  }

  List<Widget> _indicators() {
    List<Widget> list = [];
    if (typeStatus == 5) {
      list.add(
        Expanded(
          child: Align(
            child: Indicator(
              color: colors[0],
              text: '${model.newFixOrder}',
              title: '新任务',
            ),
          ),
        ),
      );
      list.add(
        Expanded(
          child: Align(
            child: Indicator(
              color: colors[1],
              text: '${model.unFixOrder}',
              title: '已接任务',
            ),
          ),
        ),
      );
      list.add(
        Expanded(
          child: Align(
            child: Indicator(
              color: colors[2],
              text: '${model.fixingOrder}',
              title: '安装中',
            ),
          ),
        ),
      );
      list.add(
        Expanded(
          child: Align(
            child: Indicator(
              color: colors[3],
              text: '${model.fixedOrder}',
              title: '已安装',
            ),
          ),
        ),
      );
    } else if (typeStatus == 2) {
      list.add(
        Expanded(
          child: Align(
            child: Indicator(
              color: colors[0],
              text: '${model.newFixOrder}',
              title: '新任务',
            ),
          ),
        ),
      );
      list.add(
        Expanded(
          child: Align(
            child: Indicator(
              color: colors[1],
              text: '${model.unFixOrder}',
              title: '已接任务',
            ),
          ),
        ),
      );
      list.add(
        Expanded(
          child: Align(
            child: Indicator(
              color: colors[2],
              text: '${model.fixingOrder}',
              title: '维修中',
            ),
          ),
        ),
      );
      list.add(
        Expanded(
          child: Align(
            child: Indicator(
              color: colors[3],
              text: '${model.fixedOrder}',
              title: '已维修',
            ),
          ),
        ),
      );
    } else if (typeStatus == 1) {
      list.add(
        Expanded(
          child: Align(
            child: Indicator(
              color: colors[0],
              text: '${model.normalNumber}',
              title: '正常',
            ),
          ),
        ),
      );
      list.add(
        Expanded(
          child: Align(
            child: Indicator(
              color: colors[1],
              text: '${model.fixNumber}',
              title: '自修',
            ),
          ),
        ),
      );
      list.add(
        Expanded(
          child: Align(
            child: Indicator(
              color: colors[2],
              text: '${model.generalNumber}',
              title: '隐患',
            ),
          ),
        ),
      );
      list.add(
        Expanded(
          child: Align(
            child: Indicator(
              color: colors[4],
              text: '${model.seriousNumber}',
              title: '维修',
            ),
          ),
        ),
      );
    } else if (typeStatus == 6) {
      list.add(
        Expanded(
          child: Align(
            child: Indicator(
              color: colors[3],
              text: '${model.normalNumber}',
              title: '审核通过',
            ),
          ),
        ),
      );
      list.add(
        Expanded(
          child: Align(
            child: Indicator(
              color: colors[0],
              text: '${model.generalNumber}',
              title: '未审核',
            ),
          ),
        ),
      );
      list.add(
        Expanded(
          child: Align(
            child: Indicator(
              color: colors[4],
              text: '${model.seriousNumber}',
              title: '审核拒绝',
            ),
          ),
        ),
      );
      list.add(
        Expanded(
          child: Align(
            child: Indicator(
              color: colors[1],
              text: '${model.unCheck}',
              title: '未抄表',
            ),
          ),
        ),
      );
    } else if (typeStatus == 7) {
      list.add(
        Expanded(
          child: Align(
            child: Indicator(
              color: colors[0],
              text: '${model.unCheck}',
              title: '进行中',
            ),
          ),
        ),
      );
      list.add(
        Expanded(
          child: Align(
            child: Indicator(
              color: colors[3],
              text: '${model.allCheck}',
              title: '已完成',
            ),
          ),
        ),
      );
    } else {
      list.add(
        Expanded(
          child: Align(
            child: Indicator(
              color: colors[0],
              text: '${model.normalNumber}',
              title: '正常',
            ),
          ),
        ),
      );
      list.add(
        Expanded(
          child: Align(
            child: Indicator(
              color: colors[1],
              text: '${model.fixNumber}',
              title: '自修',
            ),
          ),
        ),
      );
      list.add(
        Expanded(
          child: Align(
            child: Indicator(
              color: colors[2],
              text: '${model.generalNumber}',
              title: '隐患',
            ),
          ),
        ),
      );
      list.add(
        Expanded(
          child: Align(
            child: Indicator(
              color: colors[4],
              text: '${model.seriousNumber}',
              title: '维修',
            ),
          ),
        ),
      );
    }
    return list;
  }

  @override
  Widget build(BuildContext context) {
    return Row(
      children: [
        Expanded(
          child: PieOutsideLabelChart(
            _createData(),
            animate: false,
            arcRatio: 0.4,
          ),
        ),
        Padding(
          padding: EdgeInsets.symmetric(vertical: 20),
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            crossAxisAlignment: CrossAxisAlignment.start,
            children: _indicators(),
          ),
        ),
      ],
    );
  }
}

class CountLeaderPieWidget extends StatelessWidget {
  final CountLeaderModel model;

  const CountLeaderPieWidget(this.model);

  List<charts.Series<LinearSales, String>> _createData() {
    List<LinearSales> data = [];
    data.add(LinearSales(
        '现金', double.tryParse(model.cashCount) ?? 0, chartsColor[1]));
    data.add(
        LinearSales('微信', double.tryParse(model.wxCount) ?? 0, chartsColor[0]));

    // data.add(LinearSales(
    //     '农行APP', double.tryParse(model.nhCount) ?? 0, chartsColor[2]));

    double total = 0;
    for (LinearSales s in data) total += s.sales;

    data = total != 0
        ? data
        : [LinearSales('全部', 1, charts.Color(r: 108, g: 153, b: 250, a: 255))];

    return [
      new charts.Series<LinearSales, String>(
        id: 'Sales',
        domainFn: (LinearSales sales, _) => sales.title,
        measureFn: (LinearSales sales, _) => sales.sales,
        data: data,
        // Set a label accessor to control the text of the arc label.
        colorFn: (LinearSales row, _) => row.colors,
        outsideLabelStyleAccessorFn: (LinearSales row, _) =>
            charts.TextStyleSpec(
          color: charts.Color(r: 108, g: 153, b: 250, a: 255),
        ),
      )
    ];
  }

  List<Widget> _indicators() {
    List<Widget> list = [];
    list.add(
      Expanded(
        child: Align(
          child: Indicator(
            color: colors[1],
            text: '${model.cashCount}笔',
            title: Platform.isAndroid ? '现金       ' : '现金      ',
          ),
        ),
      ),
    );
    list.add(
      Expanded(
        child: Align(
          child: Indicator(
            color: colors[0],
            text: '${model.wxCount}笔',
            title: Platform.isAndroid ? '微信       ' : '微信      ',
          ),
        ),
      ),
    );

    // list.add(
    //   Expanded(
    //     child: Align(
    //       child: Indicator(
    //         color: colors[2],
    //         text: '${model.nhCount}笔',
    //         title: '农行APP',
    //       ),
    //     ),
    //   ),
    // );
    return list;
  }

  @override
  Widget build(BuildContext context) {
    return Row(
      children: [
        Expanded(
          child: PieOutsideLabelChart(
            _createData(),
            animate: false,
            arcRatio: 0.4,
          ),
        ),
        Padding(
          padding: EdgeInsets.symmetric(vertical: 20),
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            crossAxisAlignment: CrossAxisAlignment.start,
            children: _indicators(),
          ),
        ),
      ],
    );
  }
}

class Indicator extends StatelessWidget {
  final Color color;
  final String text;
  final String title;
  final double padding;
  final TextStyle textStyle;

  const Indicator({
    this.color,
    this.text,
    this.title,
    this.padding = 20,
    this.textStyle = TextThemeConfig.Font15WithColor22,
  });

  @override
  Widget build(BuildContext context) {
    return Row(
      children: [
        Container(
          height: 10,
          width: 10,
          margin: EdgeInsets.only(right: 5),
          decoration: BoxDecoration(
            color: color,
            borderRadius: BorderRadius.all(Radius.circular(5)),
          ),
        ),
        Text(
          title,
          style: TextThemeConfig.Font14WithColor66,
        ),
        Padding(
          padding: EdgeInsets.only(left: padding),
          child: Text(
            text,
            style: textStyle,
          ),
        ),
      ],
    );
  }
}
